/**
 * 工具类
 */
var tools = {

	/**
	 * 判断非空
	 * @param val
	 * @returns {Boolean}
	 */
	isEmpty: function(val) {

		val = $.trim(val);
		if (val == null || val == "null")
			return true;
		if (val == undefined || val == 'undefined')
			return true;
		if (val == "")
			return true;
		if (val.length == 0)
			return true;
		if (!/[^(^\s*)|(\s*$)]/.test(val))
			return true;
		return false;
	},
	
	isNotEmpty: function(val) {
		
		var that = this;
		return !that.isEmpty(val);
	},

	/*loading*/
	showLoading: function() {

		var that = this;
		var LOADING_HTML = [
                "<div class='com-loading' id='J-comLoading'>",
                	"<div class='ui-ico-loading'></div>",
                "</div>"].join("");
		
		that.loadingObj = $(LOADING_HTML);
		
		$("body").append(that.loadingObj);
	},
	/**
	 * 隐藏Loading
	 */
	hideLoading: function() {

		var that = this;
		
		that.loadingObj.remove();
	},
	
	/**
	 * 禁止窗体选中
	 */
	forbiddenSelect: function() {

		$(document).bind("selectstart", function() {
			return false;
		});
		document.onselectstart = new Function("event.returnValue=false;");
		$("*").css({
			"-moz-user-select" : "none"
		});
	},
	
	/*窗体允许选中 */
	autoSelect: function() {

		$(document).bind("selectstart", function() {
			return true;
		});
		document.onselectstart = new Function("event.returnValue=true;");
		$("*").css({
			"-moz-user-select" : ""
		});
	},
	
	/**
	 * 移动
	 * @param {object} moveBar 移动控件
	 * @param {object} reference 移动参照，以该对象为对照，计算移动距离,否则以moveBar自身为参照
	 */
	move: function(options) {
		
		var that = this;
		
		var opts = options;
		
		var isStarted = false;
		
		opts.moveBar.mousedown(function(e){

			var box = opts.moveBar;
			
			if(opts.reference) {
				box = opts.reference;
			}
			var y = e.clientY;
			var x = e.clientX;
			var flag = true;
			var left = box.get(0).getBoundingClientRect().left;
			var top = box.get(0).getBoundingClientRect().top;

			that.forbiddenSelect();

			$(document).mousemove(function(e){
				if(!flag){
					return;
				}
				var winW = $(window).width();
				var winH = $(window).height();
				var objW = box.width();
				var objH = box.height();
				var newL = left + e.clientX - x;
				var newT = top + e.clientY - y;
				var newR = winW - objW;/*右边最大距离*/
				var newB = winH - objH;/*下边最大距离*/
				if(2 > newT){
					newT = 2;
				}/*限制顶部*/
				if(2 > newL){
					newL = 2;
				}/*限制左边*/
				if(newL > newR){
					newL = newR - 5;
				}/*限制底部*/
				if(newT > newB){
					newT = newB - 5;
				}/*限制右边*/
				box.css({"left":newL,"top":newT});
				
				if(typeof opts.startCallback === "function" && !isStarted) {
					opts.startCallback();
					isStarted = true;
				}
			}).mouseup(function(e){

				that.autoSelect();
				/*松开鼠标停止*/
				flag = false;
				
				if(typeof opts.endCallback === "function") {
					opts.endCallback();
				}
			});
		});
	},

	/**
	 * 封闭ajax
	 * @param {string} type 请求方式
	 * @param {string} url 请求地址
	 * @param {Object} data 请求参数
	 * @param {Function} error 错误回调
	 * @param {Function} success 成功回调
	 */
	tmAjax: function(options) {

		var opts = options;
		
		$.ajax({
			type: opts.type || "post",
			url: opts.url,
			data: opts.data || {},
			error: function(err) {
				
				if(typeof opts.error === "function") {
					
					opts.error(err);
				}
			},
			success: function(data) {

				if(typeof opts.success === "function") {
					
					opts.success(data);
				}
			}
		});
	},
	
	/**
	 * 提示
	 * @param {string} content 提示内容
	 * @param {int} timeout 定时隐藏
	 */
	tip: function(options) {
		
		var opts = options || {};
		var tipEle = tools.tip.tipEle;
		
		var timeout = opts.timeout || 1;
		
		if(!tipEle) {

			var TIP_TEMP = [
	                "<div style='max-width:70%;min-width:200px;max-height:80%;padding:15px;text-align:center;background:rgba(0,0,0,0.8);font-size:14px;color:#fff;position:fixed;top:50%;left:50%;border-radius:5px;opacity:0;filter:alpha(opacity=0);z-index:99999;'>",
	                	"<span></span>",
	                "</div>"
	            ].join("");
			
			tools.tip.tipEle = tipEle = $(TIP_TEMP);
			
			$("body").append(tipEle);
		}
		
		tipEle.find("span").html(opts.content || "");

		tipEle.css({
			marginTop: (tipEle.height() + 30) / 2 * -1,
			marginLeft: (tipEle.width() + 30) / 2 * -1,
			opacity: 1
		}).show();
		
		tipEle.on("click", function() {
			tipEle.hide();
			clearTimeout(tools.tip.timer);
		});
		
			
		clearTimeout(tools.tip.timer);
		
		tools.tip.timer = setTimeout(function() {
			tipEle.hide();
		},timeout * 1000);
	}
};